package com.ruoyi.framework.interceptor;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.management.remote.rmi._RMIConnection_Stub;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AppUserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println(        request.getRequestURI());
        System.out.println(request.getRequestURL());
        //被拦截下来的请求
        //判断用户是否已经登录。
        HttpSession session = request.getSession();
        SysUser  sysUser = (SysUser) session.getAttribute("loginUser");
        if (sysUser ==null  ){
            try {
                response.getWriter().write("Unauthorized");
                response.setStatus(401);
            }
            catch (Exception e ){
                e.printStackTrace();
            }
        }
        return false;
    }
}
